package com.whw.cbf_qimodazuoye.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.whw.cbf_qimodazuoye.R;
import com.whw.cbf_qimodazuoye.entity.Hotel;
import com.whw.cbf_qimodazuoye.entity.HotelSearchResultAdapter;
import com.whw.cbf_qimodazuoye.service.HotelService;
import com.whw.cbf_qimodazuoye.utils.FaSongQingQiu;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class RecommendationActivity extends AppCompatActivity {
    GridView gridView;
    private ArrayList<Hotel> hotelArrayList = new ArrayList<>();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recommendation);
        Bundle bundle = this.getIntent().getExtras();
        final String userId = bundle.getString("userId");
        final String userName = bundle.getString("userName");
        final String userIntegral = bundle.getString("userIntegral");
        initRecommHotelList();
        gridView = findViewById(R.id.recomm_gridView);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Hotel hotel = hotelArrayList.get(position);
                Toast.makeText(RecommendationActivity.this,hotel.getHotelName(),Toast.LENGTH_LONG).show();
                Bundle bundle = new Bundle();
                bundle.putString("hotelName",hotel.getHotelName());
                bundle.putString("hotelIndex",hotel.getHotelIndex());
                bundle.putString("hotelPrice",hotel.getHotelPrice().toString());
                bundle.putString("hotelCoverpic",hotel.getHotelCoverpic());
                bundle.putString("hotelIndexpic",hotel.getHotelIndexpic());
                bundle.putString("HotelId",hotel.getHotelId().toString());
                bundle.putString("userId",userId);
                bundle.putString("userName",userName);
                bundle.putString("userIntegral",userIntegral);
                Intent intent = new Intent(RecommendationActivity.this, HotelActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        BottomNavigationView navView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home,  R.id.navigation_notifications)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(navView, navController);
        navView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.navigation_home:{
                        Bundle bundle = new Bundle();
                        bundle.putString("userId",userId);
                        bundle.putString("userName",userName);
                        bundle.putString("userIntegral",userIntegral);
                        Intent intent=new Intent(RecommendationActivity.this,MainActivity.class);
                        intent.putExtras(bundle);
                        startActivity(intent);
                        return true;
                    }


                    case R.id.navigation_notifications:{
                        Bundle bundle = new Bundle();
                        bundle.putString("userId",userId);
                        bundle.putString("userName",userName);
                        bundle.putString("userIntegral",userIntegral);
                        Intent intent=new Intent(RecommendationActivity.this,MineActity.class);
                        intent.putExtras(bundle);
                        startActivity(intent);
                        return true;
                    }
                    case R.id.navigation_dashboard:{

                        return true;
                    }

                }
                return false;
            }
        });
    }

    private void initRecommHotelList(){
        Retrofit retrofit = FaSongQingQiu.createRetrofit();
        HotelService hotelService = retrofit.create(HotelService.class);
        Call<ResponseBody> call = hotelService.selectByHotelRecommendation(1);
        call.enqueue(new Callback<ResponseBody>() {
            //请求成功时回调
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                //请求处理,输出结果
                try {
                    JSONObject jsonObject = (JSONObject) JSON.parse(response.body().string());
                    String msg =jsonObject.getString("msg");
                    String result = jsonObject.getString("result");
                    String returnobj = jsonObject.getString("obj");
                    JSONArray objArray=JSONArray.parseArray(returnobj);

                    if (result.equals("true")){
                        int a=0;
                        for (Iterator<Object> iterator = objArray.iterator(); iterator.hasNext(); ) {
                            a++;
                            JSONObject next = (JSONObject) iterator.next();
                            Hotel hotel = new Hotel();
                            hotel.setHotelId(Integer.parseInt(next.getString("hotelId")));
                            hotel.setHotelName(next.getString("hotelName"));
                            hotel.setHotelPrice(Long.parseLong(next.getString("hotelPrice")));
                            hotel.setHotelNumber(Integer.parseInt(next.getString("hotelNumber")));
                            hotel.setHotelIndex(next.getString("hotelIndex"));
                            hotel.setHotelCoverpic(next.getString("hotelCoverpic"));
                            hotel.setHotelIndexpic(next.getString("hotelIndexpic"));
                            hotel.setHotelStatus(Integer.parseInt(next.getString("hotelStatus")));
                            hotel.setHotelLabel(next.getString("hotelLabel"));
                            hotelArrayList.add(hotel);
                        }
                        HotelSearchResultAdapter hotelSearchResultAdapter = new HotelSearchResultAdapter(RecommendationActivity.this,R.layout.search_result_item,hotelArrayList);
                        gridView.setAdapter(hotelSearchResultAdapter);
                        System.out.println("请求成功！查询到"+a+"条数据");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //JsonObject jsonObject = new JsonObject()
            }
            //请求失败时候的回调
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable throwable) {
                System.out.println("连接失败");
            }
        });
    }
}
